<template>
  <div class="fullscreen-container">
    <div class="content-wrapper">
      <el-card class="login-card" width="400px">
      <template #header>
        <div class="login-title">在线考试系统</div>
      </template>
      <el-form :model="loginForm" :rules="rules" ref="loginFormRef" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名" prefix-icon="User" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" prefix-icon="Lock" />
        </el-form-item>
        <el-form-item label="角色" prop="roleId">
          <el-select v-model="loginForm.roleId" placeholder="请选择角色">
            <el-option label="管理员" value="1" />
            <el-option label="教师" value="2" />
            <el-option label="考生" value="3" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleLogin" style="width: 100%">登录</el-button>
        </el-form-item>
        <el-form-item>
          <div style="text-align: center; margin-top: 10px;">
            还没有账号？<el-link type="primary" @click="goToRegister">立即注册</el-link>
          </div>
        </el-form-item>
      </el-form>
      </el-card>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { postForm } from '../utils/api'

const router = useRouter()
const loginFormRef = ref()

const loginForm = reactive({
  username: '',
  password: '',
  roleId: ''
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ],
  roleId: [
    { required: true, message: '请选择角色', trigger: 'change' }
  ]
}

const handleLogin = async () => {
  try {
    // 表单验证
    await loginFormRef.value.validate()
    
    // 使用API工具类发送登录请求
    const result = await postForm('/user/login', {
      username: loginForm.username,
      password: loginForm.password,
      roleId: loginForm.roleId
    })
    
    if (result.code === 200) {
      // 登录成功，存储用户信息到本地存储
      const userData = result.data
      localStorage.setItem('userInfo', JSON.stringify({
        username: userData.username,
        userId: userData.userId,
        roleId: userData.roleId,
        realName: userData.realName,
        roleName: userData.roleId === 1 ? '管理员' : userData.roleId === 2 ? '教师' : '考生'
      }))
      
      // 弹出欢迎气泡
      ElMessage.success(`欢迎${userData.username}`)
      
      // 根据角色跳转到对应页面
      if (userData.roleId === 1) {
        router.push('/admin/dashboard')
      } else if (userData.roleId === 2) {
        router.push('/teacher/dashboard')
      } else if (userData.roleId === 3) {
        router.push('/student/dashboard')
      }
    } else {
      // 登录失败，显示失败信息
      ElMessage.error(result.msg || '登录失败，请重试')
    }
  } catch (error) {
    console.error('登录请求失败:', error)
    ElMessage.error('网络错误，请检查后端服务是否正常运行')
  }
}

const goToRegister = () => {
  router.push('/register')
}
</script>

<style scoped>
.fullscreen-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background-color: #f0f2f5;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
  overflow: auto;
}

.content-wrapper {
  width: 100%;
  max-width: 1200px;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.login-card {
  background: white;
  width: 600px;
  max-width: 100%;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  border-radius: 8px;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .fullscreen-container {
    padding: 10px;
  }
  
  .login-card {
    width: 100%;
  }
}

.login-title {
  text-align: center;
  font-size: 24px;
  font-weight: bold;
  padding: 30px 0;
  margin: 0;
  color: #303133;
}

/* 表单样式 */
.el-form {
  padding: 0 40px 30px;
}

.el-form-item {
  margin-bottom: 24px;
}

.el-form-item__label {
  width: 80px;
  font-size: 14px;
}

.el-form-item__content {
  margin-left: 80px !important;
}

.el-input__wrapper,
.el-select__wrapper {
  border-radius: 4px;
  font-size: 14px;
}

.el-input__inner,
.el-select__input {
  height: 36px;
  font-size: 14px;
}

.el-button--primary {
  width: 100%;
  height: 36px;
  font-size: 14px;
  margin-top: 10px;
}
</style>